home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2105 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.3 KB  |  85 lines

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Doesn't work, Why EXACTLY?
  5. Date: Mon, 15 Jan 1996 23:17:51 GMT
  6. Organization: Netcom
  7. Message-ID: <30fadc88.273651712@nntp.ix.netcom.com>
  8. References: <30FA8289.7AD6@bangate.compaq.com>
  9. NNTP-Posting-Host: ix-dc16-15.ix.netcom.com
  10. X-NETCOM-Date: Mon Jan 15  3:17:28 PM PST 1996
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. Saurabh Dixit <saurabhd@bangate.compaq.com> wrote:
  14.  
  15. |>class X {
  16. |>};
  17. |>class Y : public X {
  18. |>};
  19. |>
  20. |>class Base {
  21. |>public:
  22. |>    void func1(int a);
  23. |>    void func2(X* x);
  24. |>};
  25. |>
  26. |>class Derived : public Base {
  27. |>public:
  28. |>    void func1(float b);
  29. |>    void func2(Y* y);
  30. |>};
  31. |>
  32. |>when I call func2 from a Derived object with a pointer
  33. |>to an X object, i get - "... cannot convert from X to Y...".
  34. |>So looks like compiler looks at method func2() in Derived
  35. |>class.  I would think the signature of the call would make
  36. |>it look in Base class.
  37. |>On the lines of func1() i thought it would work.
  38. |>Please note that I don't want func2() to be virtual.
  39. |>I need func2() to do to distinct operations depending on
  40. |>what type of object was fed to it?
  41. |>
  42. |>Can anyone please explain FULLY?!?!?!
  43.  
  44. That's the way C++ works.  Overloading only takes place within a scope
  45. and a derived class is a different scope than the base class.  From
  46. the draft (13.2)
  47.  
  48.         Two function declarations of the same name refer to the same
  49. function
  50.         if they are in the same scope and have equivalent parameter
  51.         declarations.  A function member of a derived class is not in
  52. the
  53.         same scope as a function member of the same name in a base
  54. class.
  55.  
  56.         [Example:
  57.  
  58.           class B {
  59.           public:
  60.               int f(int);
  61.           };
  62.  
  63.           class D : public B {
  64.           public:
  65.               int f(char*);
  66.           };
  67.  
  68.         Here D::f(char*) hides B::f(int) rather than overloading it.
  69.  
  70.           void h(D* pd)
  71.           {
  72.               pd->f(1);       // error:
  73.                               // D::f(char*) hides B::f(int)
  74.               pd->B::f(1);    // ok
  75.               pd->f("Ben");   // ok, calls D::f
  76.           }
  77.  
  78. The ARM contains the same language.
  79.  
  80. If your compiler supports namespaces, you can incorporate the function
  81. from the base class into the derived class with a using declaration.
  82.  
  83.  
  84. Michael M Rubenstein
  85.